<?php

namespace core;

use core\system\HttpRequest as HttpRequest;
use core\util\Constant as Constant;
use ReflectionClass;

class CoreRouter {

    private static $controller;
    private static $action;
    private static $service;

    public static function loader() {
        self::$controller = (empty(HttpRequest::GET("mod"))) ? Main::index()[Constant::$SECTION]["mod"] : HttpRequest::GET("mod");
        self::$action = (empty(HttpRequest::GET("action"))) ? Main::index()[Constant::$SECTION]["action"] : HttpRequest::GET("action");
        self::$service = (empty(HttpRequest::GET("service"))) ? false : true;
        $auth = system\AuthInterceptor\AuthInterceptor::callAuthen();
        if (!$auth) {
            return $auth;
        }
        CoreRouter::getController();
    }

    private static function getController() {
        $mod = (self::$service ? Constant::$SERVICE : Constant::$SECTION);
        $fileController = 'application.controller.' . $mod . '.' . ucwords(self::$controller) . 'Controller';
        \core\CoreBase::import($fileController);
        try {
            $class = new ReflectionClass('\application\controller\\' . $mod . '\\' . ucwords(self::$controller) . 'Controller');
            $controllerName = $class->getNamespaceName() . '\\' . self::$controller . 'Controller';
            $actionName = strtolower(self::$action);
            $controllerLoad = new $controllerName();
            $controllerLoad->$actionName();
        } catch (Exception $ex) {
            die('Invalid view method : ' . self::$action);
        }
    }

}
